﻿package com.kaka.web_maven.controller;

import com.kaka.web_maven.dao.UsersDao;
import com.kaka.web_maven.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


@WebServlet(name = "UserAddServlet", urlPatterns = "/UserAddServlet")
public class UserAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 获取数据
            String username = request.getParameter("username")
            int age = Integer.parseInt(request.getParameter("age"));

            Date birth = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("birth"));

           
            Users user = new Users();
            user.setUsername(username);
            user.setAge(age);
            user.setBirth(birth);

            UsersDao dao = new UsersDao();
            dao.save(user);

            // 页面转发到显示所有的列表的页面
            request.getRequestDispatcher("/UsersServlet").forward(request,response);

        } catch (Exception e){
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
